﻿using System;
using System.Web.UI;

namespace UCF.Core.Web
{
    /// <summary>
    /// 客户端脚本管理对象
    /// </summary>
    public class JScriptManager
    {
        /// <summary>
        /// 在body内容最下面添加一个script脚本块
        /// </summary>
        /// <param name="page"></param>
        /// <param name="script">不包含脚本标签的JavaScript脚本</param>
        public static void RegisterStartupScript(Page page, string script)
        {
            if (script.Length == 0) return;
            if (script.Substring(0, 7) != "<script") script = string.Format("<script>{0}</script>", script);
            page.ClientScript.RegisterStartupScript(page.GetType(), page.UniqueID + "".GetRandomString(), script);
        }
        /// <summary>
        /// 在body内容最上面添加一个script脚本块
        /// </summary>
        /// <param name="page"></param>
        /// <param name="script"></param>
        public static void RegisterClientScriptBlock(Page page, string script)
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), page.UniqueID + "".GetRandomString(), script);
        }
        /// <summary>
        /// 在head区域最下面添加一个jQuery.Ready()页面载入脚本
        /// </summary>
        /// <param name="page"></param>
        /// <param name="script"></param>
        public static void CreateJqueryDocumentReadyScript(System.Web.UI.Page page, string script)
        {
            CreateHeadScriptBlock(page, "jQuery(document).ready(function($){" + script + "});");
        }
        /// <summary>
        /// 在head区域最下面添加一个脚本块
        /// </summary>
        /// <param name="page"></param>
        /// <param name="script"></param>
        public static void CreateHeadScriptBlock(Page page, string script)
        {
            LiteralControl lcScript = new LiteralControl(Environment.NewLine + "<script>" + Environment.NewLine + script + Environment.NewLine + "</script>" + Environment.NewLine);
            page.Header.Controls.Add(lcScript);
        }
    }
}
